import VueI18n from 'vue-i18n'
import 'normalize.css/normalize.css'
import 'babel-polyfill'

import MakingForm from './components/Container.vue'
import GenerateForm from './components/GenerateForm.vue'
import GenerateAntdForm from './components/AntdvGenerator/GenerateForm.vue'

import enUS from './lang/en-US'
import zhCN from './lang/zh-CN'

import './iconfont/iconfont.css'
import './styles/cover.scss'
import './styles/index.scss'
import './styles/editor.scss'
import './styles/fm.scss'

const loadLang = function (Vue, lang, locale, i18n) {
  if (locale) {
    locale('en-US', {...locale('en-US'), ...enUS})
    locale('zh-CN', {...locale('zh-CN'), ...zhCN})
    Vue.config.lang = lang
  } else if (i18n) {
    i18n.setLocaleMessage('en-US', {...i18n.messages['en-US'], ...enUS})
    i18n.setLocaleMessage('zh-CN', {...i18n.messages['zh-CN'], ...zhCN})
    i18n.locale = lang
  } else {
    Vue.use(VueI18n)
    Vue.locale('en-US', {...Vue.locale('en-US'), ...enUS})
    Vue.locale('zh-CN', {...Vue.locale('zh-CN'), ...zhCN})
    Vue.config.lang = lang
  }
}

const expire = 253402271999000
const version = '1.3.6'

const loadOptions = (opts) => {
  window.FormMaking_OPTIONS = {
    ...opts,
    key: '0420200302008',
    version
  }
}

MakingForm.install = function (Vue, opts = {
  lang: 'zh-CN',
  locale: null,
  i18n: null
}) {
  loadLang(Vue, opts.lang, opts.locale, opts.i18n)
  if (expire >= new Date().getTime()) {
    Vue.component(MakingForm.name, MakingForm)
  }
  loadOptions(opts)
}

GenerateForm.install = function (Vue, opts = {
  lang: 'zh-CN',
  locale: null,
  i18n: null
}) {
  loadLang(Vue, opts.lang, opts.locale, opts.i18n)
  if (expire >= new Date().getTime()) {
    Vue.component(GenerateForm.name, GenerateForm)
  }
  loadOptions(opts)
}

GenerateAntdForm.install = function (Vue, opts = {
  lang: 'zh-CN',
  locale: null,
  i18n: null
}) {
  loadLang(Vue, opts.lang, opts.locale, opts.i18n)
  if (expire >= new Date().getTime()) {
    Vue.component(GenerateAntdForm.name, GenerateAntdForm)
  }
  loadOptions(opts)
}

const components = [
  MakingForm,
  GenerateForm,
  GenerateAntdForm
]

const install = function (Vue, opts = {
  lang: 'zh-CN',
  locale: null,
  i18n: null,
  components: []
}) {
  opts = {
    key: '0420200302008',
    lang: 'zh-CN',
    locale: null,
    i18n: null,
    components: [],
    ...opts
  }

  loadLang(Vue, opts.lang, opts.locale, opts.i18n)

  if (expire >= new Date().getTime()) {
    components.forEach(component => {
      Vue.component(component.name, component)
    })

    opts.components && opts.components.forEach(item => {
      Vue.component(item.name, item.component)
    })
  }
  loadOptions(opts)
}

if (typeof window !== 'undefined' && window.Vue) {
  install(window.Vue)
}

export {
  install,
  MakingForm,
  GenerateForm,
  GenerateAntdForm
}

export default {
  install,
  MakingForm,
  GenerateForm,
  GenerateAntdForm
}
